from pyecharts.charts import Bar
from pyecharts.options import TitleOpts, TextStyleOpts

from utils import create_connection

conn = create_connection(database='hrs')
try:
    with conn.cursor() as cursor:
        cursor.execute('select dname, total from vi_dept_emp_count')
        x_data, y_data = [], []
        for dname, total in cursor.fetchall():
            x_data.append(dname)
            y_data.append(total)
        bar_chart = Bar()
        title_options = TitleOpts(
            title='部门人数统计',
            pos_left='65',
            title_textstyle_opts=TextStyleOpts(
                color='blue',
                font_size='32',
                font_family='FZJKai-Z03S'
            )
        )
        bar_chart.set_global_opts(title_opts=title_options)
        bar_chart.add_xaxis(x_data)
        bar_chart.add_yaxis('人数', y_data)
        bar_chart.render()
finally:
    conn.close()
